/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var TgSpecialTopicRModel = function(obj){
    ItbBaseModel.call(this,obj);

    // 以下为表定义数据，需要的字段就填入
    //`st_id` bigint(20) unsigned NOT NULL COMMENT '专题ID',
    //`type_id` bigint(20) NOT NULL COMMENT '分类: 1单页, 2多页, 3ppt',
    //`item_id` bigint(20) NOT NULL COMMENT '(设计)模版ID',
    //`order` int(3) NOT NULL DEFAULT '999' COMMENT '显示顺序',
    //`create_time` datetime NOT NULL,
    //`update_time` datetime NOT NULL,
    //`update_by` varchar(50) NOT NULL DEFAULT '',
    //`del_flg` int(1) NOT NULL DEFAULT '0'

};
TgSpecialTopicRModel.prototype = new ItbBaseModel();

// 取得表名
TgSpecialTopicRModel.prototype.getTableName = function(){
    return 'tg_special_topic_r';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
TgSpecialTopicRModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'st_id='+this.st_id+' and item_id='+this.item_id+ 'and type_id='+this.type_id;
}

//------------------------------------------------------------------
// 清除指定模版的专题信息
TgSpecialTopicRModel.prototype.delDocTopic = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = 'UPDATE tg_special_topic_r '+
            ' SET del_flg=1, update_time=sysdate(), update_by= '+THIS.update_by+
            ' WHERE del_flg = 0 AND item_id='+ THIS.item_id+
            ' AND type_id='+THIS.type_id;


        //var sqlValue = [THIS.update_by, THIS.doc_id];
        //var sql = mysql.format(sql, sqlValue);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            if(error) {
                callback(error);
            } else {
                callback(null);
            }
        });
    });
}

//------------------------------------------------------------------

// 导出
module.exports = TgSpecialTopicRModel;